/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import javax.microedition.lcdui.Graphics;

/**
 *
 * @author kien.tran
 */
public class DropDownItem extends AFormItem {

    private Style valueStyle, buttonStyle;
    private String value;
    private int buttonWidth = 16;

    public DropDownItem(String id, String title, String value) {
        super(id, title);
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void paint(int x, int y, Graphics g) {
        paint(x, y, g, false);
    }

    public void paint(int x, int y, Graphics g, boolean validate) {
        if (validate && !isPaint(x, y)) {
            return;
        }

        // pre-calc
        calculateHeight();
        if (titleRows == null) {
            setTitleRows(title);
        }

        // paint background
        g.setColor(focus == false ? style.getBgColor() : style.getSelectedBgColor());
        g.fillRect(x, y, width, height);

        // paint title
        int i, tempX, tempY, tempW, tempH, borderColor, bgColor;
        if (title.length() != 0) {
            tempX = x + style.getPadding().getLeft() + titleStyle.getPadding().getLeft();
            for (i = 0; i < titleRows.length; i++) {
                tempY = y + style.getPadding().getTop() + i * (font.getFontBold().getHeight() + titleStyle.getPadding().getTop());
//                boldFont.drawString(titleRows[i], tempX, tempY, Font2.LEFT, g);
                font.getFontBold().drawString(g, titleRows[i], tempX, tempY, Graphics.TOP | Graphics.LEFT);
            }
        }
        borderColor = (focus == false ? valueStyle.getBorderColor() : valueStyle.getSelectedBorderColor());
        bgColor = (focus == false ? valueStyle.getBgColor() : valueStyle.getSelectedBgColor());
        tempX = x + style.getPadding().getLeft();
        tempY = y + style.getPadding().getTop() + titleStyle.getHeight();
        tempW = width - style.getPadding().getLeftAndRight();
        tempH = valueStyle.getHeight();
        paintBox(tempX, tempY, tempW, tempH, borderColor, bgColor, g);

        // paint ...
        bgColor = (focus == false ? buttonStyle.getBgColor() : buttonStyle.getSelectedBgColor());
        borderColor = (focus == false ? buttonStyle.getBorderColor() : buttonStyle.getSelectedBorderColor());
        paintDot(width - style.getPadding().getRight() - buttonWidth + 1, tempY + 1, buttonWidth, tempH - 2, bgColor, borderColor, g);

        // paint value
        tempX = tempX + 1 + valueStyle.getPadding().getLeft();
        tempY = tempY + 1 + valueStyle.getPadding().getTop();
        tempW = tempW - valueStyle.getPadding().getLeftAndRight();
        if (value != null && value.length() > 0) {
//            normalFont.drawString(normalFont.splitWith(value, tempW)[0], tempX, tempY, Font2.LEFT, g);
            font.getFontNormal().drawString(g, value, tempX, tempY, Graphics.TOP | Graphics.LEFT);
        }
    }

    protected void calculateHeight() {
        if (titleRows == null) {
            setTitleRows(title);
        }

        if (title.length() > 0) {
            titleStyle.setHeight(titleRows.length * (font.getFontBold().getHeight() + titleStyle.getPadding().getTop()) + titleStyle.getPadding().getBottom());
        } else {
            titleStyle.setHeight(0);
        }

        valueStyle.setHeight(font.getFontNormal().getHeight() + valueStyle.getPadding().getTopAndBottom() + 2);

        this.height = titleStyle.getHeight() + valueStyle.getHeight() + style.getPadding().getTopAndBottom();
    }

    protected void setTitleRows(String title) {
        if (title.length() > 0) {
            int w = width - style.getPadding().getLeftAndRight() - titleStyle.getPadding().getLeftAndRight() - buttonWidth;
//            titleRows = boldFont.splitWith(title, w);
            titleRows = new String[]{title};
        } else {
            titleRows = new String[]{""};
        }
    }

    private void paintBox(int x, int y, int width, int height, int borderColor, int bgColor, Graphics g) {
        g.setColor(borderColor);
        g.drawRect(x, y, width - 1, height - 1);
        g.setColor(bgColor);
        g.fillRect(x + 1, y + 1, width - 2, height - 2);
    }

    private void paintDot(int x, int y, int width, int height, int bgColor, int dotColor, Graphics g) {
        g.setColor(bgColor);
        g.fillRect(x, y, width, height);
        g.setColor(dotColor);
        g.fillRect(x + 3, y + height - 5, 2, 2);
        g.fillRect(x + 7, y + height - 5, 2, 2);
        g.fillRect(x + 11, y + height - 5, 2, 2);
    }

    public void setParent(ACanvas parent) {
        super.setParent(parent);
        if (parent != null) {
            this.valueStyle = parent.getStyle().getTextBoxStyle();
            this.buttonStyle = parent.getStyle().getButtonStyle();
        }
    }
}
